<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

require_once dirname(__FILE__).'/menu.php';

class hd_invitation_customer extends PC_controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('Hdinvitationcustomermodel','m_customer');
    }

    public function index(){
        $data = parent::index();

        global $g_hd_invitation_gift_type;
        $data['game_gift'] = array4js($g_hd_invitation_gift_type);
        $data['includeExcel'] = 1;

        $this->load->view('admin/hd_invitation_customer',$data);
    }

    public function getCustomerList(){
        $info = $this->input->post();
        $assist['where'] = getFilter($this->input->post(),Hdinvitationcustomermodel::$searchField);
        $assist['like'] = getFilter($this->input->post(),Hdinvitationcustomermodel::$likeField);
        $assist['sort'] = getSort($this->input->post(),Hdinvitationcustomermodel::$sortField);
        $assist['pagination'] = getPagination($this->input->post());

        // if(isset($info['gift_status']) && $info['gift_status'] == 0) {
        //     $assist['where']['hd_invitation_customer.gift_type >'] ='0';
        // }

        if(isset($assist['where']['hd_invitation_customer.gift_type']) && $assist['where']['hd_invitation_customer.gift_type'] < 0) {
            unset($assist['where']['hd_invitation_customer.gift_type']);
            $assist['where']['hd_invitation_customer.gift_type >'] ='0';
        }
        if(isset($assist['where']['hd_invitation_customer.gift_type']) && $assist['where']['hd_invitation_customer.gift_type'] >= 1000) {
            if($assist['where']['hd_invitation_customer.gift_type'] == 1000) {
                $assist['where']['hd_invitation_customer.lottery_times'] = 0;
                $assist['where']['hd_invitation_customer.gift_type'] ='0';
            } else if($assist['where']['hd_invitation_customer.gift_type'] == 1001) {
                $assist['where']['hd_invitation_customer.lottery_times >'] =0;
                $assist['where']['hd_invitation_customer.gift_type'] ='0';
            } else if($assist['where']['hd_invitation_customer.gift_type'] == 1002) {
                unset($assist['where']['hd_invitation_customer.gift_type']);
                $assist['where']['hd_invitation_customer.lottery_times >'] =0;
            }
        }

        list($list,$count) = $this->m_customer->getCustomerListSection($assist);
        // $customer_ids = extractColumn($list,'customer_id');
        // $card_list = $this->m_customer->getCardList($customer_ids);
        // $card_list = spreadArray($card_list,'customer_id');

        foreach ($list as $key => $value) {
            $list[$key]['nickname'] = urldecode($value['nickname']);
            // $list[$key]['card_list'] = isset($card_list[$value['customer_id']]) ? $card_list[$value['customer_id']] : array();
        }

        $this->response->setSuccess();
        $this->response->setItems($list);
        $this->response->setTotalCount($count);
        $this->response->responseExit();
    }

    public function exportCustomerList() {
        @set_time_limit(0);
        @ini_set('memory_limit', '3048M');

        $info = $this->input->get();
        $assist['where'] = getFilter($this->input->get(),Hdinvitationcustomermodel::$searchField);
        $assist['like'] = getFilter($this->input->get(),Hdinvitationcustomermodel::$likeField);
        $assist['sort'] = getSort($this->input->get(),Hdinvitationcustomermodel::$sortField);

        if(isset($assist['where']['hd_invitation_customer.gift_type']) && $assist['where']['hd_invitation_customer.gift_type'] < 0) {
            unset($assist['where']['hd_invitation_customer.gift_type']);
            $assist['where']['hd_invitation_customer.gift_type >'] ='0';
        }
        if(isset($assist['where']['hd_invitation_customer.gift_type']) && $assist['where']['hd_invitation_customer.gift_type'] >= 1000) {
            if($assist['where']['hd_invitation_customer.gift_type'] == 1000) {
                $assist['where']['hd_invitation_customer.lottery_times'] = 0;
                $assist['where']['hd_invitation_customer.gift_type'] ='0';
            } else if($assist['where']['hd_invitation_customer.gift_type'] == 1001) {
                $assist['where']['hd_invitation_customer.lottery_times >'] =0;
                $assist['where']['hd_invitation_customer.gift_type'] ='0';
            } else if($assist['where']['hd_invitation_customer.gift_type'] == 1002) {
                unset($assist['where']['hd_invitation_customer.gift_type']);
                $assist['where']['hd_invitation_customer.lottery_times >'] =0;
            }
        }

        $this->load->library('zmexcel');
        $this->zmexcel->init();
        // $this->zmexcel->addSheet('广汽本田9月区域特色售后服务活动用户');
        $this->zmexcel->setTitle(array(
            array('content'=>'ID'),
            array('content'=>'openid'),
            array('content'=>'奖品'),
            array('content'=>'奖品状态'),
            array('content'=>'卡券号'),
            array('content'=>'抽奖次数'),
            array('content'=>'创建时间'),
            array('content'=>'获奖时间'),
            array('content'=>'姓名'),
            array('content'=>'车架号'),
            array('content'=>'初始化来源'),
        ));

        list($list,$count) = $this->m_customer->getCustomerListSection($assist);
        if(count($list) > 8000) {
            echo '导出数据太大，请筛选数据导出!'.count($list);
            die();
        }

        // $card_list = $this->m_customer->getCardList();
        // $card_list = spreadArray($card_list,'customer_id');

        // foreach ($list as $key => $value) {
        //     $list[$key]['nickname'] = urldecode($value['nickname']);
        //     $list[$key]['card_list'] = isset($card_list[$value['customer_id']]) ? $card_list[$value['customer_id']] : array();
        // }

        global $g_hd_invitation_gift_type;

        $sex_array = array(
            0 => '未知',
            1 => '男',
            2 => '女',
        );
        foreach ($list as $key => $value) {
            $this->zmexcel->addRow(array(
                array('content' => $value['customer_id']),
                array('content' => $value['openid']),
                array('content' => id2name($value['gift_type'],$g_hd_invitation_gift_type)),
                array('content' => $value['gift_type'] == 0 ? '' :($value['gift_status'] == STATUS_ACTIVE ? '已核销' : '未核销')),
                array('content' => $value['gift_code']),
                array('content' => $value['lottery_times']),
                array('content' => $value['customer_time_create']),
                array('content' => $value['customer_time_lottery']),
                array('content' => $value['customer_name']),
                array('content' => $value['customer_car_number']),
                array('content' => $value['create_source']),
            ));
            unset($list[$key]);
        }

        // $this->excel->freezePanes(1,0);
        $this->zmexcel->generate('广汽本田9月区域特色售后服务活动用户_'.date('YmdHis').'.xls');
    }

    public function disableCustomer() {
        $this->validate->setRule('customer_id','integer');
        $this->validate->setRule('customer_id','message',$this->lang->line('error_parameter'));
        if(!$this->validate->run($this->input->post())){
            $this->response->setDetail($this->validate->getMessage());
            $this->response->responseExit();
        }
        $info = $this->validate->getResult();

        $info['customer_status'] = STATUS_DELETE;
        $this->m_customer->editCustomer($info);

        $this->response->setSuccess();
        $this->response->setDetail('更新成功!');
        $this->response->responseExit();
    }

    public function enableCustomer() {
        $this->validate->setRule('customer_id','integer');
        $this->validate->setRule('customer_id','message',$this->lang->line('error_parameter'));
        if(!$this->validate->run($this->input->post())){
            $this->response->setDetail($this->validate->getMessage());
            $this->response->responseExit();
        }
        $info = $this->validate->getResult();

        $info['customer_status'] = STATUS_DISABLE;
        $this->m_customer->editCustomer($info);

        $this->response->setSuccess();
        $this->response->setDetail('更新成功!');
        $this->response->responseExit();
    }
}
/* End of file customer.php */
/* Location: ./application/controllers/customer.php */
